package com.it.myh.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GeolifeGridPartition {

    public static void main(String[] args) {
        String datasetPath = "C:\\Users\\IoV321\\Desktop\\qwe.txt";
        int gridRows = 200; // 网格行数
        int gridCols = 200; // 网格列数
        int numGroups = 100; // 网格分组数

        try {
            // 读取Geolife数据集
            BufferedReader reader = new BufferedReader(new FileReader(datasetPath));
            String line;
            Map<String, Integer> gridCountMap = new HashMap<>();
            Map<String, GridPoint> gridPointMap = new HashMap<>();
            while ((line = reader.readLine()) != null) {
                // 解析每个位置点的经纬度信息
                String[] parts = line.split(";");
                double latitude = Double.parseDouble(parts[0].substring(9));
                double longitude = Double.parseDouble(parts[1].substring(10));

                // 计算网格坐标
                int gridX = (int) (longitude * gridCols / 360.0);
                int gridY = (int) (latitude * gridRows / 180.0);

                // 统计每个网格中的轨迹点数量
                String gridKey = gridX + "," + gridY;
                gridCountMap.put(gridKey, gridCountMap.getOrDefault(gridKey, 0) + 1);

                // 记录每个网格中心位置的坐标
                String centerKey = gridX + "," + gridY;
                if (!gridPointMap.containsKey(centerKey)) {
                    double centerLatitude = (gridY + 0.5) * (180.0 / gridRows);
                    double centerLongitude = (gridX + 0.5) * (360.0 / gridCols);
                    gridPointMap.put(centerKey, new GridPoint(centerLatitude, centerLongitude));
                }
            }
            reader.close();

            // 输出每个网格中心位置的坐标和轨迹点数量
            int groupSize = gridCountMap.size() / numGroups;
            int count = 0;
            int groupCount = 1;
            for (Map.Entry<String, GridPoint> entry : gridPointMap.entrySet()) {
                String centerKey = entry.getKey();
                GridPoint centerPoint = entry.getValue();
                int pointCount = gridCountMap.get(centerKey);
                System.out.println("Group: " + groupCount + ", Grid: " + centerKey + ", Center: " +
                        centerPoint.getLatitude() + ", " + centerPoint.getLongitude() + ", Count: " + pointCount);

                count++;
                if (count >= groupSize) {
                    count = 0;
                    groupCount++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 网格中心点的坐标类
    static class GridPoint {
        private double latitude;
        private double longitude;

        public GridPoint(double latitude, double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public double getLatitude() {
            return latitude;
        }

        public double getLongitude() {
            return longitude;
        }
    }
}


